package com.tuteng.administrator.ashospital;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.tuteng.administrator.ashospital.Activity.UserRegisterActivity;
import com.tuteng.administrator.ashospital.Manager.NetBool;
import com.tuteng.administrator.ashospital.application.hospitalAppication;
import com.tuteng.administrator.ashospital.beans.BindCardResponse;
import com.tuteng.administrator.ashospital.beans.LandReqBean;
import com.tuteng.administrator.ashospital.beans.UserBean;
import com.tuteng.administrator.ashospital.beans.landBean;
import com.tuteng.administrator.ashospital.beans.usertest;
import com.tuteng.administrator.ashospital.comprehensive.LoadingDialogManager;
import com.tuteng.administrator.ashospital.comprehensive.checkutils;

import cn.jpush.android.api.JPushInterface;

import static cn.jpush.im.android.api.jmrtc.JMRTCInternalUse.getApplicationContext;
import static com.tuteng.administrator.ashospital.Config.GlobleConfig.APP_URL;

/**
 * Created by Administrator on 2017/12/9.
 */

public class ThreeFragment extends Fragment implements  View.OnClickListener {
    private Context contex;
    private RelativeLayout relativeLayout;
    private  View mReLand;
    private EditText mUserId;
    private EditText mUserpassword;
    private TextView mForgetpassword;
    private TextView mNewUser;
    private hospitalAppication app;
    private FrameLayout mFland;
    private  FrameLayout mFuscenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.uc_land,container,false);
        contex = container.getContext();

        inintView(view);
      initData();
       // TextView content= (TextView) view.findViewById(R.id.content);
    //    content.setText("This is ThreeFragment");
        return view;
    }

    private void inintView(View view) {
        mUserId=(EditText)view.findViewById(R.id.edt_uc_land_user);

        mUserpassword=(EditText)view.findViewById(R.id.edt_uc_land_pass);
        mForgetpassword=(TextView)view.findViewById(R.id.tv_uc_forget_pws);
        mNewUser=(TextView) view.findViewById(R.id.tv_uc_new_user);
        mReLand=view.findViewById(R.id.re_ucland_langding);
        mReLand.setOnClickListener(this);
        mForgetpassword.setOnClickListener(this);
        mNewUser.setOnClickListener(this);
        app=hospitalAppication.getInstance();
        mFland=view.findViewById(R.id.fm_uc_land);
        mFuscenter=view.findViewById(R.id.fm_us_center);
        if(app.getResponse()==null)
        {mFland.setVisibility(View.VISIBLE);
         mFuscenter.setVisibility(View.GONE);

        }

        else {mFland.setVisibility(View.GONE);
            mFuscenter.setVisibility(View.VISIBLE);}
    }

    private void initData() {

    }

    @Override
    public void onClick(View v) {
 switch (v.getId()){
     case R.id.re_ucland_langding:

System.out.print("---------->");
         mReLand.setBackgroundResource(R.mipmap.btn_press);
        // Log.e("----->",""+NetBool.isNetworkAvailable(UCLandingActivity.this));
         if(!NetBool.isNetworkAvailable(contex)){
             Toast.makeText(contex,R.string.net_contact_false,Toast.LENGTH_SHORT).show();
         }

         else{
             iniintNetData();
         }
     case  R.id.tv_uc_new_user:
         Intent intent = new Intent(contex,MainActivity.class);
         contex.startActivity(intent);
         break;
         default:break;
 }
    }

    private void iniintNetData() {
        String rid = JPushInterface.getRegistrationID(getContext());
        landBean landBean=new landBean();
       landBean.setRegistrationID(rid);
        landBean.setMethodid("0");
        landBean.setPhone(mUserId.getText().toString());
         landBean.setPassword( mUserpassword.getText().toString());
         String neturl=APP_URL+"/land";
        final String js= JSON.toJSONString(landBean);

        OkGo.<BindCardResponse>post(neturl)
                .tag(this)
                .upJson(js)
                .execute(new JsonCallback<BindCardResponse>() {
                    @Override
                    public void onStart(Request<BindCardResponse, ? extends Request> request) {
                        super.onStart(request);
                    }

                    @Override
                    public void onFinish() {
                        LoadingDialogManager.getInstance().dismissDialog();
                    }

                    @Override
                    public void onSuccess(Response<BindCardResponse> response) {
                        BindCardResponse u=response.body();
                     if(!u.getResultCode().equals("0"))
                     {
                         Toast.makeText(contex,u.getResultMsg(),Toast.LENGTH_LONG).show();
                         return;
                     }
                   else {
                         InputMethodManager imm = (InputMethodManager)contex. getSystemService(Context.INPUT_METHOD_SERVICE);
                         imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                          app.setResponse(u);


                  mFland.setVisibility(View.GONE);
                  mFuscenter.setVisibility(View.VISIBLE);

                   /**
                        mFourFragment=new FourFragment();
                         ft = manager.beginTransaction();

//当前的fragment会被myJDEditFragment替换
                         ft.replace(R.id.fl,mFourFragment);
                         ft.addToBackStack(null);
                         ft.commit();
  **/
                         //FragmentTransaction trx = contex.getSupportFragmentManager().beginTransaction();
                     }
                       // System.out.print(u.getAge()+"------->"+u.getName());
                    }
                });

    }

    private boolean checkInputValid() {
        if (mUserId.getText().toString().isEmpty()) {
            Toast.makeText(contex, "账号不能为空", Toast.LENGTH_SHORT).show();
            mUserId.requestFocus();
            return false;
        }
        if (!checkutils.checkLength(mUserpassword, "密码")) {
            return false;
        }
        return true;
    }

}